package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.pojo.DeviceType;
import com.example.medical.entity.pojo.NoticeType;
import com.example.medical.service.NoticeTypeService;
import com.example.medical.mapper.NoticeTypeMapper;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.stereotype.Service;

/**
* @author 24354
* @description 针对表【notice_type】的数据库操作Service实现
* @createDate 2024-03-31 22:42:21
*/
@Service
public class NoticeTypeServiceImpl extends ServiceImpl<NoticeTypeMapper, NoticeType>
    implements NoticeTypeService{

    @Override
    public Result AdminGetNoticeTypeList(GetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        IPage<NoticeType> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<NoticeType> wrapper=new LambdaQueryWrapper<>();
        if (dto.getMemo()!=null) {
            wrapper.like(NoticeType::getNoticeTypeName,dto.getMemo());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public Result AdminRemoveNoticeTypeArr(String[] ids) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        for (String id : ids) {
            boolean checkRemove = removeById(id);
            if (!checkRemove) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminUpdateNoticeType(Long id, String name) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        NoticeType checkNoticeType = getById(id);
        if (checkNoticeType==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        checkNoticeType.setNoticeTypeName(name);
        boolean checkUpdate = updateById(checkNoticeType);
        if (!checkUpdate) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }
}




